// pages/examination/examination.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',//课程id
    timer:null,//定时器
    items:'',//答题卡信息
    num:0,//题目的索引
    detail:'',//题目详情  type:1 2//单选题 3 5 多问 4翻译
    daid:'',//答题卡ID
    time:'',//考试时间
    tabid:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      time:options.time
    })
    this.init()
    this.minReturn(60*Number(this.data.time))
  },
  init(){//创建答题卡
    let params ={
      question_id:wx.$cache.get('question_id'),
      curriculum_id:this.data.id,
      school_id:wx.$cache.get('schoolid'),
      major_id:wx.$cache.get('majorid'),
    }
    wx.$http.post(wx.$get.add_assessment_answer_card,params).then(res=>{
      console.log(res,'创建答题卡')
      this.setData({
        daid:res
      })
      this.chainit()
    })
  },
  chainit(){//查看答题卡信息
    let params={
      question_id:wx.$cache.get('question_id'),
      curriculum_id:this.data.id
    }
    wx.$http.post(wx.$get.my_assessment_answer_card,params).then(res=>{
      console.log(res,'答题卡信息',res.card_json)
      this.setData({
        items:res.card_json
      })
      this.detail()
    })
  },
  detail(){//题目详情
    let params = {
      member_id:wx.$cache.get('member_id'),
      subject_id:this.data.items[this.data.num].subject_id
    }
    wx.$http.post(wx.$get.assessment_subject_info,params).then(res=>{
      console.log(res,'题目详情')
      this.setData({
        detail:res
      })
    })
  },
  jiao(){//交卷提交答题卡
    var that =this
    let {items} = this.data
    let zong=[]//一共的题数
    let arr = []//做了的题数
    for(let i=0;i<items.length;i++){
      if(items[i].type==4||items[i].type==1||items[i].type==2){
        console.log('1')
        zong = zong.concat('1')
        if(items[i].my_answer||items[i].score_fraction){
          arr = arr.concat('1')
          console.log(2)
        }
      }else{
        for(let j=0;j<items[i].little_question_json.length;j++){
          zong = zong.concat('1')
          console.log(3)
          if(items[i].little_question_json[j].my_answer||items[i].little_question_json[j].score_fraction){
            arr = arr.concat('1')
            console.log(4)
          }
        }
      }
    }
    let params = {
      answer_card_id:this.data.daid,
      card_json:JSON.stringify(this.data.items)
    }
    wx.showModal({
      title: '提示',
      content: `你还有${zong.length-arr.length}题未做完,确定交卷？`,
      success (res) {
        if (res.confirm) {
          wx.$http.post(wx.$get.submit_assessment_answer_card,params).then(res=>{
            console.log(res,'提交答题卡')
            wx.redirectTo({
              url: `/pages/examreport/examreport?id=${that.data.id}`,
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  shang(){//上一题
    this.setData({
      num:this.data.num-=1,
      tabid:0
    })
    this.detail()
  },
  next(){//下一题
    this.setData({
      num:this.data.num+=1,
      tabid:0
    })
    this.detail()
  },
  sure(){//确认答案的//赋值到答题卡上面去
    let {type} = this.data.detail
    let {items} = this.data
    if(type==1 || type==2){//复制选项到答题卡
      let pan = this.selectComponent('#exam').data.detail.option_type
      let array2 = []
      if(pan!=2){
        let array = this.selectComponent('#exam').data.detail.option_str
        let array3=[] //存做的答案
        for(let i =0;i<array.length;i++){
          if(array[i].flage){
            array2 = array2.concat(array[i].option)
            array3 = array3.concat(array[i].option)
          }else{
            array3 = array3.concat('空')
          }
        }
        items[this.data.num].array3 = array3
      }
      if(pan!=1){
        let array = this.selectComponent('#exam').data.detail.option_img
        let array3=[] //存做的答案
        for(let i =0;i<array.length;i++){
          if(array[i].flage){
            array2 = array2.concat(array[i].option)
            array3 = array3.concat(array[i].option)
          }else{
            array3 = array3.concat('空')
          }
        }
        items[this.data.num].array4 = array3
      }
      items[this.data.num].my_answer = array2.toString()
      this.setData({
        items
      })
    }else if(type==4 ||type==6){//赋值翻译题分数
      let fen = this.selectComponent('#exam2').data.fen
      items[this.data.num].score_fraction = fen
      this.setData({
        items
      })
    }else if(type==3){
      let array2 = ''//选择的答案
      // console.log(this.selectComponent('#exam3').data)
      let tabid = this.selectComponent('#exam3').data.tabid
      let array = this.selectComponent('#exam3').data.detail.little_question_json[tabid].option_str
      let arrayimg = this.selectComponent('#exam3').data.detail.little_question_json[tabid].option_img
      for(let i =0;i<array.length;i++){
        if(array[i].flage){
          array2 = array[i].option
        }
      }
      for(let i =0;i<arrayimg.length;i++){
        if(arrayimg[i].flage){
          array2 = arrayimg[i].option
        }
      }
      items[this.data.num].little_question_json[tabid].my_answer = array2
      this.setData({
        items
      })
    }else if(type==5){
      let tabid = this.selectComponent('#exam3').data.tabid
      let fen = this.selectComponent('#exam3').data.fen
      items[this.data.num].little_question_json[tabid].score_fraction = fen
      this.setData({
        items
      })
    }
  },
  minReturn (time) {//倒计时用的
    let {timer} = this.data
    timer = setInterval(() => {
      time--
      let moveMin = this.formatSeconds(time)
      wx.setNavigationBarTitle({
        title: moveMin
      })
      this.setData({
        timer,
        time
      })
      if (time <= 0) {
        clearInterval(this.data.timer)
        //交卷
      }
    }, 1000)
  },
  formatBit (val) {
    val = +val
    return val > 9 ? val : '0' + val
  },
　　 // 秒转时分秒，求模很重要，数字的下舍入
  formatSeconds (time) {
    let min = Math.floor(time % 3600)
    let val = this.formatBit(Math.floor(time / 3600)) + ':' + this.formatBit(Math.floor(min / 60)) + ':' + this.formatBit(time % 60)
    return val
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.time>0){
      
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // clearInterval(this.data.timer)
    console.log('3')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer)
    console.log(4)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})